package cn.fbs.service;


import cn.fbs.config.UploadDataProperties;
import cn.fbs.config.UploadImageProperties;
import cn.fbs.config.UploadVideoProperties;
import cn.fbs.util.imageshort.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Service
@Slf4j
public class UploadService {
    @Autowired
    private UploadImageProperties imageProperties;
    @Autowired
    private UploadVideoProperties videoProperties;
    @Autowired
    private UploadDataProperties dataProperties;


    /**
     * 上传图片
     *
     * @param file
     * @return
     */
    public String uploadImage(MultipartFile file) throws IOException {
        if (file.getSize()>1024*1024*1024){
//        if (file.getSize()>1024*1024){
            return "500,文件大小不能超过1GB";
        }
        //校验文件类型
        String contentType = file.getContentType();
        List<String> allowTypes = imageProperties.getAllowTypes();
        if (!allowTypes.contains(contentType)) {
            throw new IOException("文件类型不匹配，仅支持" + allowTypes.toString());
        }
        File tempFile = new File(file.getOriginalFilename());
        String path = imageProperties.getDoestUrl()+System.currentTimeMillis();
        mkdir(path);
        path=path+"/"+ tempFile.getName();
        log.info(path);
        path = isRepeat(path);
        //准备目标路径
        File doest = new File(path);
//        file.transferTo(doest);
        this.transferTo(file,doest);

        //返回路径
        /**
         * 如果是tif，生成同名不同后缀的图片
         */
        String suf = path.substring(path.lastIndexOf(".") + 1);
        if (suf.equalsIgnoreCase("tif")||suf.equalsIgnoreCase("tiff")){
            String pngPath=path.substring(0,path.lastIndexOf("."))+".png";
            FileUtil.getCompressBase64FromUrl2(new File(path),5,pngPath);
        }
        return path;

    }


    /**
     * 上传资料
     *
     * @param file
     * @return
     */
    public String uploadData(MultipartFile file) throws IOException {
        if (file.getSize()>1024*1024*1024){
            return "500,文件大小不能超过1GB";
        }
        //校验文件类型
        String contentType = file.getContentType();
        List<String> allowTypes = dataProperties.getAllowTypes();
        log.info("文件类型"+contentType);
        if (!allowTypes.contains(contentType)) {
            throw new IOException("文件类型不匹配，仅支持" + allowTypes.toString());
        }
        //准备目标路径
        String path = dataProperties.getDoestUrl()
                + System.currentTimeMillis();
        mkdir(path);
        path = path + "/" + file.getOriginalFilename();
        path = isRepeat(path);
        File doest = new File(path);
        this.transferTo(file,doest);

//        file.transferTo(doest);
        //返回路径
        return path;

    }

    /**
     * 视频文件上传
     *
     * @param file
     * @return
     */
    public String uploadVideo(MultipartFile file) throws IOException {
        if (file.getSize()>1024*1024*1024){
            return "500,文件大小不能超过1GB";
        }
        //校验文件类型
        String contentType = file.getContentType();
        List<String> allowTypes = videoProperties.getAllowTypes();
        if (!allowTypes.contains(contentType)) {
            throw new IOException("文件类型不匹配，仅支持" + allowTypes.toString());
        }
        //准备目标路径
        String path = videoProperties.getDoestUrl() +System.currentTimeMillis();
        mkdir(path);
        path=path+"/"+ file.getOriginalFilename();
        path = isRepeat(path);
        File doest = new File(path);
        //保存文件到本地
        this.transferTo(file,doest);
//        file.transferTo(doest);
        //返回路径
        return path;
    }

    private void transferTo(MultipartFile file, File doest) {
        try {
            byte[] bytes = file.getBytes();
            FileOutputStream fileOutputStream = new FileOutputStream(doest);
            fileOutputStream.write(bytes);
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String isRepeat(String path) {
        File file = new File(path);
        if (file.exists()) {
            path = path.substring(0, path.lastIndexOf(".")) + System.currentTimeMillis() + path.substring(path.lastIndexOf("."));
            path = isRepeat(path);
        }
        return path;
    }

    public String uploadXml(MultipartFile file,String fileType) {
        //校验文件类型
        String contentType = file.getContentType();
        //准备目标路径
        String path =null;
        if ("video".equalsIgnoreCase(fileType)){
             path = videoProperties.getDoestUrl() +System.currentTimeMillis();
        }
        if ("image".equalsIgnoreCase(fileType)){
            path = imageProperties.getDoestUrl() +System.currentTimeMillis();
        }
        mkdir(path);
        path=path+"/"+ file.getOriginalFilename();
        path = isRepeat(path);
        File doest = new File(path);
        //保存文件到本地
//            file.transferTo(doest);
        this.transferTo(file,doest);

        //返回路径
        return path;
    }
    private void mkdir(String path){
        File file = new File(path);
        if (!file.exists()){
            file.mkdir();
        }
    }
}
